package top.zuli.package03.exam01;

import java.util.Arrays;

public class ArraysDemo22 {
    public static void main(String[] args) {
        User5[] User5s = new User5[4];
        for (int i = 0; i <User5s.length; i++) {
            User5 User5 = new User5();
            User5.setId(i+1);
            User5.setName("张三"+(i+1));
            User5.setAge((int)(Math.random()*100));
            User5s[i] = User5;
        }
        Arrays.sort(User5s);
        System.out.println(Arrays.toString(User5s));
    }
}
class User5 implements Comparable{
    private int id;
    private String name;
    private int age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User5{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Object o) {
        if(o==null){
            throw new NullPointerException("null不能参与排序");
        }
        if(!(o instanceof  User5)){
            throw new ClassCastException();
        }
        User5 User5 = (User5)o;
        return this.age-User5.getAge();
    }
}
